/*
 * 文  件   名: FileInfoManagerImpl.java
 * 版         本 : docrepository-server(Anttribe). All rights reserved.
 * 描         述 : <描述>
 * 修   改  人: zhaoyong
 * 修改时间: 2014年10月29日
 */
package org.anttribe.docrepository.file.manager.impl;

import java.util.List;

import org.anttribe.docrepository.common.MagicNumber;
import org.anttribe.docrepository.common.UnifyException;
import org.anttribe.docrepository.common.errorno.SystemErrorNumber;
import org.anttribe.docrepository.file.dao.FileInfoDao;
import org.anttribe.docrepository.file.domain.FileInfo;
import org.anttribe.docrepository.file.manager.FileInfoManager;
import org.anttribe.saturn.common.utils.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author zhaoyong
 * @version 2014年10月29日
 */
public class FileInfoManagerImpl implements FileInfoManager
{
    /**
     * LOGGER
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(FileInfoManagerImpl.class);
    
    /**
     * fileInfoDao
     */
    private FileInfoDao fileInfoDao;
    
    @Override
    public List<FileInfo> listFileByParent(String parentId, String ownerId)
        throws UnifyException
    {
        if (StringUtils.isEmpty(ownerId))
        {
            LOGGER.warn("While querying, ownerId is null.");
            // 参数不正确
            throw new UnifyException(SystemErrorNumber.PARAMETER_IS_NULL);
        }
        
        return fileInfoDao.queryByOwnerAndParent(ownerId, parentId);
    }
    
    @Override
    public List<FileInfo> listFavoriteFileByUser(String userId)
        throws UnifyException
    {
        if (StringUtils.isEmpty(userId))
        {
            LOGGER.warn("While querying, userId is null.");
            // 参数不正确
            throw new UnifyException(SystemErrorNumber.PARAMETER_IS_NULL);
        }
        
        return fileInfoDao.queryByFavorite(userId);
    }
    
    @Override
    public void saveOrUpdateFileInfo(FileInfo fileinfo)
        throws UnifyException
    {
        if (ObjectUtils.isNullOrNone(fileinfo))
        {
            LOGGER.warn("While saving or updating, fileinfo is null.");
            // 参数不正确
            throw new UnifyException(SystemErrorNumber.PARAMETER_IS_NULL);
        }
        
        // 首先进行更新数据，如果更新影响行数为0，表示数据不存在，那么就保存数据
        int affectRows = this.fileInfoDao.update(fileinfo);
        if (MagicNumber.NUMBER_ZERO == affectRows)
        {
            this.fileInfoDao.insert(fileinfo);
        }
    }
    
    public FileInfoDao getFileInfoDao()
    {
        return fileInfoDao;
    }
    
    public void setFileInfoDao(FileInfoDao fileInfoDao)
    {
        this.fileInfoDao = fileInfoDao;
    }
}